<?php
/**
	* 区域详细, 左右值无限分类
	*
	* @package Package Name
	* 表名 ". $this->f."catagory
	* 调用前必须先执行 setfix函数,对前缀进行设置
	
	控制器调用实例
	
	header("content-type:text/html;charset=utf-8");
	$this->load->model('sort_model');
	$this->sort_model->setfix('jiaju_area_');
	$getsonid = $this->sort_model->getsonid('6');
	foreach($getsonid as $row):
		echo $row['CatagoryID'].'<br />';
	endforeach;

*/
class Sort_model extends Model {
	var $f; // 表的前缀 catagory
	
	function __construct()
	{
        parent::Model();
        $this->load->database();  //加载数据库
	}
	
	
	// ------------------------------------------------------------------------
	/**
		* 获得表的前缀
	*/
	public function setfix($fix)
	{
		$this->f = $fix;
	}
	
	
	
	// ------------------------------------------------------------------------
	/**
		* 根据ID 查询出左右值
		* return array
	*/
	public function getLR($CatagoryID)
	{
		$sql = "SELECT Lft,Rgt FROM ". $this->f."catagory WHERE CatagoryID=$CatagoryID";
		$query = $this->db->query($sql);		
		return $query->row();
	}
	
	
	
	// ------------------------------------------------------------------------
	/**
		* 左右值，根据ID读取这个ID和这个ID下的所有子ID
	*/
	public function getsonid($CatagoryID)
	{
		$result=array();
		$LR = $this->getLR($CatagoryID);
		$Lft = $LR->Lft;
		$Rgt = $LR->Rgt;
		$sql = "SELECT CatagoryID FROM ". $this->f."catagory WHERE Lft BETWEEN $Lft AND $Rgt ORDER BY Lft ASC";
		$query = $this->db->query($sql);
		foreach($query->result() as $row):
			$result[] = $row->CatagoryID;
		endforeach;
		return $result;
	}
	
	
	// ------------------------------------------------------------------------
	/**
		* 转换： 将父ID转成左右值
		* 量大要分批进行 否则PHP要超时 每次处理1000到1200条吧
		* SQL 语句要改的
		控制器调用	
		function abc(){
			$this->load->model('sort_model');
			$this->sort_model->setfix('jiaju_class_');
			//注意： 在运行之前必须先手动在库中把一级分类建了  如 ID 1  名称  “家具分类”
			$getsonid = $this->sort_model->swicthLR();  
		}
	*/
	public function swicthLR()
	{
		$sql = "select * from jiaju_class  order by classid asc";
		$query = $this->db->query($sql);
		foreach ($query->result() as $value):	
			$this->addsort($value->classtopid,$value->classname,$value->classid);
		endforeach;
	}
	

	// ------------------------------------------------------------------------
	/**
		* 添加分类到 左右值表
		* $selfID 这个参数主要是在转换表的时候要用到。正常添加都是自动递增的
	*/	
	function addsort($CatagoryID,$SortName,$selfID=NULL)
	{
		// 0为父id只能有一个
		//表示没有一级分类，当前分类就是一级分类,用于添加一个唯一的一级分类
		if($CatagoryID==0)
		{
			$Lft=0;
			$Rgt=1;
		}else
		{
			$LR = $this->getLR($CatagoryID);
			$Lft = $LR->Lft;
			$Rgt = $LR->Rgt;
			$this->db->query("UPDATE `".$this->f."catagory` SET `Lft`=`Lft`+2 WHERE `Lft`>$Rgt");
			$this->db->query("UPDATE `".$this->f."catagory` SET `Rgt`=`Rgt`+2 WHERE `Rgt`>=$Rgt");
		}
	
		//插入
		if ($selfID != NULL)
		{
			$this->db->query("INSERT INTO `".$this->f."catagory` SET `Lft`='$Rgt',`Rgt`='$Rgt'+1,`CatagoryID`='$selfID',`Name`='$SortName'");
		}
		else{
			$this->db->query("INSERT INTO `".$this->f."catagory` SET `Lft`='$Rgt',`Rgt`='$Rgt'+1,`Name`='$SortName'");
		}

	} // end func 

	
	// ------------------------------------------------------------------------
	/**
		* 读取左右值分类
		*
	*/
	function readcatagory($CatagoryID)
	{
		
		$result = array();
		// 获得根类的左右值
		$LR = $this->getLR($CatagoryID);
		$Lft = $LR->Lft;
		$Rgt = $LR->Rgt;
		
		$sql = "select * from ".$this->f.'catagory'." where Lft BETWEEN $Lft AND $Rgt order by Lft ASC";
		$right = array();
		foreach ($this->db->query($sql)->result_array() as $row) {
			// only check stack if there is one
			if (count($right) > 0) {
				// 检查我们是否应该将节点移出堆栈
				while ($right[count($right) - 1] < $row['Rgt']) {
					array_pop($right);
				}
			}
			// 缩进显示节点的名称
			
			$result[] = $row['CatagoryID'].str_repeat('|-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',count($right)) . $row['Name'] . "<br />\n";
			// 将这个节点加入到堆栈中
			$right[] = $row['Rgt'];
		}
		return $result;
	}// end func	
	
	
	
	
	
	
}//end class	